package br.ufrgs.seguranca.cryptography;

import java.math.BigInteger;

/**
 *
 * @author matheus
 */
public class Texto1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BigInteger n = new BigInteger("340282366920938463463374607431768211297");
        BigInteger g = new BigInteger("339661812359158752487805590648382725483");

        BigInteger X = new BigInteger("276787826956062272733680681271338069612");
        BigInteger Y = new BigInteger("84157582510126722674495911347458676013");

        BigInteger glinha = g.modInverse(n);

        System.out.println("Resolvendo Diffie-Hellman Multiplicativo");
        System.out.println();
        System.out.println("n: " + n);
        System.out.println("g: " + g);
        System.out.println("X: " + X);
        System.out.println("Y: " + Y);
        System.out.println();
        System.out.println("g' (inverso multiplicativo de g): " + glinha);

        System.out.println();
        System.out.println("Calculando k = X * Y * g' mod n");
        BigInteger k = ((X.multiply(Y)).multiply(glinha)).mod(n);
        System.out.println("k (chave): " + k);
        System.out.println();

        System.out.println("Calculando os 16 bytes do AES");
        BigInteger[] bk = new BigInteger[16];
        BigInteger dois = new BigInteger("256");

        for (int i = 0; i < 16; i++) {
            bk[i] = k.mod(dois);
            k = k.divide(dois);
        }

        System.out.print("[");
        for (int i = 0; i < 16; i++) {
            if (i < 15) {
                System.out.print(bk[i] + ", ");
            } else {
                System.out.print(bk[i]);
            }
        }
        System.out.println("]");

    }

}
